package com.acme.superlogger.slf4j;

public class MessageFormatter {

    static final String DELIM_STR_REGEX = "\\{\\}";
    static final String DELIM_STR = "{}";

    public static String format(String format, Object... args) {
        StringBuilder buffer = new StringBuilder();
        if (!format.contains(DELIM_STR) || args.length == 0) {
            return format;
        }
        // splitting
        String[] splitted = format.split(DELIM_STR_REGEX, -1);

        int i;
        int j;

        // adding [ message part - argument ]
        for (i = 0, j = 0; i < args.length && j < splitted.length - 1; i++, j++) {
            buffer.append(splitted[j]);
            buffer.append(args[i].toString());
        }
        // now adding rest format string, adding missing delims
        for (; j < splitted.length; j++) {
            buffer.append(splitted[j]);
            if (j < splitted.length - 1) {
                buffer.append(DELIM_STR);
            }
        }
        return buffer.toString();
    }
}